home *** CD-ROM | disk | FTP | other *** search
- /* lsearch on page 316 of the Turbo C Bible */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int mycompare(const void *, const void *);
- char *our_table[20] =
- {
- "Microsoft C 5.1",
- "Quick C 1.0",
- "Turbo C 2.0",
- NULL
- };
- main(int argc, char **argv)
- {
- unsigned int i, count, oldcount;
- char **p_table, **result;
- if(argc < 2)
- {
- printf("Usage: %s <KEYWORD>\n", argv[0]);
- exit(0);
- }
- /* Find length of our table and print it */
- printf("==== Our table contains ====\n");
- for(count = 0, p_table = our_table; *p_table != NULL; p_table++, count++)
- printf("%s\n", *p_table);
- oldcount = count;
- /* Search for the PATH variable in the environment */
- result = (char **) lsearch(&argv[1], our_table, &count, sizeof(char *),
- mycompare);
- if(count == oldcount)
- printf("\nFound %s in\n%s\n", argv[1], *result);
- else
- {
- printf("\n%s was added to table\n", argv[1]);
- /* Print table again */
- printf("==== Now table contains ====\n");
- for(i=0; i<count; i++)
- printf("%s\n", our_table[i]);
- }
- }
- /* ------------------------- */
- int mycompare(const void *arg1, const void *arg2)
- {
- /* Compare two strings up to the length of the key */
- return(strncmp(*(char**)arg1, *(char**)arg2, strlen(*(char**)arg1)));
- }